﻿using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Mengsk.ProxyDelivery.App.UI.Config
{
    /// <summary>
    /// Interaction logic for VIPConfigUserControl.xaml
    /// </summary>
    public partial class VIPConfigUserControl : UserControl, IPlugin
    {

        const string SERVICE_MONEY = "ServiceMoney";
        const string SERVICE_MOENY_VIP = "ServiceMoneyVIP";
        const string DELIVERY_MONEY_VIP = "DeliveryMoneyVIP";

        public VIPConfigUserControl()
        {
            InitializeComponent();
        }

        public string Title
        {
            get { return "VIP设置"; }
        }

        public Control UIControl
        {
            get { return this; }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                float s = float.Parse(this.tbServiceMoney.Text.Trim());
                float sv = float.Parse(this.tbVIPServiceMoneyScale.Text.Trim());
                float dv = float.Parse(this.tbVIPDeliveryMoneyScale.Text.Trim());

                SystemConfigService service = ServiceContainer.GetService<SystemConfigService>();

                service.SaveOrUpdate(SERVICE_MONEY, s.ToString("F1"));
                service.SaveOrUpdate(SERVICE_MOENY_VIP, sv.ToString("F1"));
                service.SaveOrUpdate(DELIVERY_MONEY_VIP, dv.ToString("F1"));

                MessageBox.Show("已保存");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                SystemConfigService service = ServiceContainer.GetService<SystemConfigService>();

                var s = service.GetByField("name", SERVICE_MONEY);
                this.tbServiceMoney.Text = s == null ? "2.5" : s.Value;

                s = service.GetByField("name", SERVICE_MOENY_VIP);
                this.tbVIPServiceMoneyScale.Text = s == null ? "0.8" : s.Value;

                s = service.GetByField("name", DELIVERY_MONEY_VIP);
                this.tbVIPDeliveryMoneyScale.Text = s == null ? "1" : s.Value;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
